/**
 * File Name   : DiscountAction.java
 * Description : 
 */
package com.hh.admin.discount.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.common.service.CommonService;
import com.hh.admin.discount.form.DiscountBean;
import com.hh.admin.discount.form.DiscountForm;
import com.hh.admin.discount.service.DiscountService;
import com.hh.admin.discounttype.form.DiscountTypeBean;
import com.hh.common.consts.AdminConst;

/**
 * 消费打折Action
 * @author 冯昌硕
 * @create 2013-3-12 下午8:10:20
 */
public class DiscountAction extends MappingDispatchAction {

    private static final Logger log = Logger.getLogger(DiscountAction.class);
    
    /**
     * 公共Service
     */
    private CommonService commonService;
    
    /**
     * 消费打折Service
     */
    private DiscountService discountService;
    
    /**
     * 后台管理-获取消费打折信息内容列表
     * @create 2013-3-12 下午8:21:06
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountForm dForm = (DiscountForm) form;
        
        // 查询当前页要显示的记录
        DiscountBean paramBean = new DiscountBean();
        List<DiscountBean> discountLst = 
                commonService.selRecordList(dForm, "discount.selDiscountList", paramBean);
        request.setAttribute("discountLst", discountLst);
        
        // 查询总记录数
        int totleCnt = commonService.selRecordCount(paramBean, "discount.selDiscountCount");
        dForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / dForm.getPageSize();
        if(totleCnt % dForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        dForm.setTotlePage(pageCnt);
        log.debug("[list] 查询消费打折信息记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 跳转到内容添加页面
     * @create 2013-3-12 下午9:19:22
     */
    public ActionForward toAdd(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountForm dForm = (DiscountForm) form;
        
        DiscountTypeBean paramBean = new DiscountTypeBean();
        List<DiscountTypeBean> discountTypeLst = commonService.selRecordList(
                dForm, 
                "discounttype.selAllDiscountType", 
                paramBean);
        request.setAttribute("discounttypeLst", discountTypeLst);
        
        return mapping.findForward("addpage");
    }
    
    /**
     * 添加一条记录
     * @create 2013-3-12 下午9:13:37
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountForm dForm = (DiscountForm) form;
        log.debug("[add] 新增消费打折内容 status=[" + dForm.getStatus() + "]");
        
        LoginUserInfo loginUserInfo = 
                (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        dForm.setCreateUser(loginUserInfo.getLoginName());
        
        int newId = discountService.insertDiscount(dForm);
        log.debug("[add] 新增消费打折内容ID=[" + newId + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * 跳转到内容编辑页面
     * @create 2013-3-12 下午9:50:57
     */
    public ActionForward detailForEdit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountForm dForm = (DiscountForm) form;
        
        DiscountTypeBean paramBean = new DiscountTypeBean();
        List<DiscountTypeBean> discountTypeLst = commonService.selRecordList(
                dForm, 
                "discounttype.selAllDiscountType", 
                paramBean);
        request.setAttribute("discounttypeLst", discountTypeLst);
        
        int id = dForm.getId();
        DiscountBean result = discountService.getDiscountDetail(id);
        request.setAttribute("discountDetail", result);
        return mapping.findForward("edit");
    }
    
    /**
     * 内容编辑后提交
     * @create 2013-3-12 下午10:12:42
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountForm dForm = (DiscountForm) form;
        int updCnt = discountService.updateDiscount(dForm);
        log.debug("修改消费打折内容[" + dForm.getId() + "]信息，修改影响行数=[" + updCnt + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * 单条内容删除
     * @create 2013-3-12 下午10:42:48
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountForm dForm = (DiscountForm) form;
        
        int id = dForm.getId();
        log.debug("消费打折内容删除，要删除的记录ID=[" + id + "]");
        int delCnt = discountService.deleteOneDiscount(id);
        log.debug("消费打折[" + id + "]删除，影响行数=[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountForm dForm = (DiscountForm) form;
        Integer[] ids = dForm.getIds();
        
        int delCnt = discountService.deleteManyDiscount(ids);
        log.debug("批量消费打折内容删除，影响行数:[" + delCnt + "]");
        
        return mapping.findForward("list");
    }

    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }

    public void setDiscountService(DiscountService discountService) {
        this.discountService = discountService;
    }
    
}
